package com.treasure.service.creditCard.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.IdCreater;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.domain.TApplyCardOrderExample;
import com.treasure.service.domain.TUserInfo;
import com.treasure.service.dto.ApplyForDto;
import com.treasure.service.dto.TApplyCardOrderDto;
import com.treasure.service.enums.BankEnum;
import com.treasure.service.enums.CodeEnum;
import com.treasure.service.mapper.TBankConfigMapper;
import com.treasure.service.mapper.TUserInfoMapper;
import com.treasure.service.third.utils.RestClient;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.treasure.service.constant.Status;
import com.treasure.service.domain.TApplyCardOrder;
import com.treasure.service.creditCard.service.OrderService;
import com.treasure.service.mapper.TApplyCardOrderMapper;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestClient restClient;

	@Value("${treasure.rest.service.pay}")
    private String thirdUrl;

    @Autowired
	private TApplyCardOrderMapper tApplyCardOrderMapper;

    @Autowired
    private TBankConfigMapper tBankConfigMapper;

    @Autowired
    private TUserInfoMapper tUserInfoMapper;
    
	@Override
	public ResponseResult createApplyCardOrder(TApplyCardOrderDto applyCardOrderDto) {

		//Todo 调用网申接口

		ApplyForDto applyForDto = new ApplyForDto();
		applyForDto.setApply_name(applyCardOrderDto.getRealName());
		applyForDto.setApply_idnum(applyCardOrderDto.getCertNo());
		applyForDto.setApply_phone(applyCardOrderDto.getMobile());
		applyForDto.setChannel(applyCardOrderDto.getChannel());
		applyForDto.setBankCode(applyCardOrderDto.getBankId()+"");
		ResponseResult responseResult = restClient.post(thirdUrl+"/creditCard/v1/applyFor",applyForDto,ResponseResult.class);

		if(responseResult.getCode().equals(CodeEnum.SUCCESS.getCode())){

			//Todo 获取银行基础返佣金额
//			TBankConfig tBankConfig = new TBankConfig();
//			tBankConfig.setBankId(applyCardOrderDto.getBankId());
//			tBankConfig =  tBankConfigMapper.selectOne(tBankConfig);

			//Todo 查询办卡人推荐人信息
			TUserInfo tUserInfo = tUserInfoMapper.selectByPrimaryKey(applyCardOrderDto.getCustomerId());
			Long inviteCode = tUserInfo.getInviteCode();
			TApplyCardOrder applyCardOrder = BeanUtil.copy(applyCardOrderDto,TApplyCardOrder::new);
			if (StringUtil.isNotEmpty(inviteCode)){
				tUserInfo = new TUserInfo();
				tUserInfo = tUserInfoMapper.selectByPrimaryKey(inviteCode);
				applyCardOrder.setInviterId(tUserInfo.getUserId());
			}

//			applyCardOrder.setCommission(tBankConfig.getCommission());
//			applyCardOrder.setCommission(0l);
			applyCardOrder.setId(IdCreater.getLongId());
			//TODO 根据openId获取当前用户信息并设置用户customerId
			applyCardOrder.setStatus(Status.ORDER.CREATED);//创建订单
			logger.info("创建信用卡订单:"+JSON.toJSONString(applyCardOrder));
			tApplyCardOrderMapper.insertSelective(applyCardOrder);
		}


		return responseResult;
	}

	@Override
	public TApplyCardOrderDto getOrderMsg(Long orderId) {

		TApplyCardOrder tApplyCardOrder =tApplyCardOrderMapper.selectByPrimaryKey(orderId);
		if (tApplyCardOrder.getStatus().intValue() <3){
			// Todo 如果订单仍处于申请中，去三方渠道查询订单状态
			ApplyForDto applyForDto = new ApplyForDto();
			applyForDto.setApply_name(tApplyCardOrder.getRealName());
			applyForDto.setApply_idnum(tApplyCardOrder.getCertNo());
			applyForDto.setApply_phone(tApplyCardOrder.getMobile());
			applyForDto.setChannel(tApplyCardOrder.getChannel());
			ResponseResult responseResult = restClient.post(thirdUrl+"/creditCard/v1/queryCreditCardSchedule",applyForDto,ResponseResult.class);
		}
		return BeanUtil.copy(tApplyCardOrder,TApplyCardOrderDto::new);

	}

	@Override
	public PageResult<List<TApplyCardOrderDto>> getCardOrderList(Integer pageSize, Integer pageNum, Long userId,String param) {

		if (StringUtil.isNotEmpty(param)){

		}

		Page page = PageHelper.startPage(pageNum,pageSize);

		TApplyCardOrderExample tApplyCardOrderExample = new TApplyCardOrderExample();
		tApplyCardOrderExample.createCriteria().andCustomerIdEqualTo(userId);
		tApplyCardOrderExample.or(tApplyCardOrderExample.createCriteria().andInviterIdEqualTo(userId));
		List<TApplyCardOrder> list = tApplyCardOrderMapper.selectByExample(tApplyCardOrderExample);
		List<TApplyCardOrderDto> result=new ArrayList<>();

		list.forEach(tApplyCardOrder -> {

			TApplyCardOrderDto tApplyCardOrderDto = BeanUtil.copy(tApplyCardOrder,TApplyCardOrderDto::new);
			tApplyCardOrderDto.setBankName(BankEnum.getBankName(tApplyCardOrderDto.getBankId()));
			result.add(tApplyCardOrderDto);

		});

		return new PageResult<>(page,page);
	}

}
